home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The 640 MEG Shareware Studio 2
/
The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO
/
clang
/
tcplusx.zip
/
EVENTMGR.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1991-02-28
|
11KB
|
391 lines
//
// eventmgr.cpp - implementation of class EventManager
// Author - Robin W. McKean
// Last Update - February 23,1991
// Copyright (C) 1991 All rights reserved
//
// This file remains the property of the author, Robin W. McKean. You are
// free to use and change it as you see fit. This module, nor its object
// code, may not however be included in any packaged software without the
// written consent of the author.
//
// Contents ----------------------------------------------------------------
//
// EventManager::EventManager
// EventManager::~EventManager
// EventManager::isA
// EventManager::nameOf
// EventManager::hashValue
// EventManager::isEqual
// EventManager::printOn
// EventManager::putEvent
// EventManager::getEvent
// EventManager::positionDevice
// EventManager::hideDevice
// EventManager::showDevice
// EventManager::addDevice
// EventManager::subtractDevice
//
// Description
//
// The purpose if this class is to command and control all of the
// devices that a programmer wants to use as input. The devices will
// feed events to the event manager, and the programmer can access
// those events by calling EventManager::getEvent( Event& ). Also, the
// programmer can put their own events into the queue by calling
// EventManager::putEvent( Event&, int ).
//
// End ---------------------------------------------------------------------
// Interface dependencies --------------------------------------------------
#ifndef _EVENTMGR_H
#include <eventmgr.h>
#endif
#ifndef _IOSTREAM_H
#include <iostream.h>
#endif
#ifndef _STDLIB_H
#include <stdlib.h>
#define _STDLIB_H
#endif
// End Interface dependencies ----------------------------------------------
// Implementation dependencies ---------------------------------------------
#ifndef _DEVICE_H
#include <device.h>
#endif
// End Implementation dependencies -----------------------------------------
// Member Function //
EventManager::EventManager( int maxEvents ) : queueList(), deviceList(), freeList()
// Description -------------------------------------------------------------
//
// The constructor allocates our events, and sets up our lists for
// for input processing
//
// End ---------------------------------------------------------------------
{
eventMax = maxEvents;
if( eventMax <= 0 || eventMax >= 100 )
eventMax = 100; // Maximum events
events = new Event[ eventMax ];
if( !events ) return; // from the heap
// Body Comment ------------------------------------------------------------
//
// We add the events from the array to the free list. This saves
// memory and allows us to insure that events are there to be processed
//
// End ---------------------------------------------------------------------
for( int i = 0; i < eventMax; i++ ) freeList.add( events[ i ] );
}
// End EventManager::EventManager //
// Member Function //
EventManager::~EventManager( void )
// Description -------------------------------------------------------------
//
// The destructor detaches all of the events from both lists, and then
// destroys the events it allocated
//
// End ---------------------------------------------------------------------
{
while( !queueList.isEmpty() )
{
Object& theObject = queueList.getLeft();
}
while( !freeList.isEmpty() )
{
Object& theOtherObject = freeList.peekAtHead();
freeList.detachFromHead( theOtherObject );
}
delete [ eventMax ]events;
}
// End EventManager::~EventManager //
// Member Function //
classType EventManager::isA( ) const
// Description -------------------------------------------------------------
//
// Returns the numeric equivelent of the class type
//
// End ---------------------------------------------------------------------
{
return eventManagerClass;
}
// End EventManager::isA //
// Member Function //
char *EventManager::nameOf( ) const
// Description -------------------------------------------------------------
//
// Returns the character string equivelent of a class
//
// End ---------------------------------------------------------------------
{
return "EventManager";
}
// End EventManager::nameOf //
// Member Function //
hashValueType EventManager::hashValue( ) const
// Description -------------------------------------------------------------
//
// There is no hash value for an event manager, so return 0.
//
// End ---------------------------------------------------------------------
{
return hashValueType( 0 );
}
// End EventManager::hashValue //
// Member Function //
#pragma argsused
int EventManager::isEqual( const Object& theEventManager ) const
// Description -------------------------------------------------------------
//
// Determines if one event manager is equivelent to another
//
// End ---------------------------------------------------------------------
{
return( 0 );
}
// End EventManager::isEqual //
// Member Function //
void EventManager::printOn( ostream& theOutputStream ) const
// Description -------------------------------------------------------------
//
// The event manager does not really have any of its own information
// to output to the screen. So what we will do is dump the device
// list, queue list, and the free list. I hope you have your pause
// button ready to be pressed.
//
// End ---------------------------------------------------------------------
{
deviceList.printOn( theOutputStream );
queueList.printOn( theOutputStream );
freeList.printOn( theOutputStream );
}
// End EventManager::printOn //
void EventManager::putEvent( Event& theEvent, int atBeginning )
// Description -------------------------------------------------------------
//
// We attempt to put this event into the queue. We first check to see
// if a free event is available. If it is, we add it at the head or
// the tail, depending on the status of atBeginning.
//
// End ---------------------------------------------------------------------
{
if( freeList.isEmpty() ) return;
Event& theNewEvent = ( Event& )freeList.peekAtHead();
freeList.detachFromHead( theNewEvent );
theNewEvent = theEvent;
if( atBeginning ) queueList.putLeft( theNewEvent );
else queueList.putRight( theNewEvent );
}
// EventManager::putEvent //
// Member Function //
void EventManager::getEvent( Event& theEvent )
// Description -------------------------------------------------------------
//
// We check to see if there is something in the queue. If there is not
// then we continually poll the devices until an event is put into our
// queue.
//
// End ---------------------------------------------------------------------
{
ContainerIterator& device = deviceList.initIterator();
while( queueList.isEmpty() )
{
while( ( int )device ) ( ( Device& )( device++ ) ).pollDevice();
device.restart();
}
Event& theNewEvent = ( Event& )queueList.getLeft( );
freeList.add( theNewEvent );
theEvent = theNewEvent;
// Must destroy the device because it is allocated from the heap
delete &device;
}
// End EventManager::Get //
// Member Function //
void EventManager::positionDevice( classType deviceType, int row, int column )
// Description -------------------------------------------------------------
//
// This function will position the requested device at the coordinates
// specified in row and column.
//
// End ---------------------------------------------------------------------
{
ContainerIterator& device = deviceList.initIterator();
while( ( int )device )
{
Device& theDevice = ( Device& )device++;
if( theDevice.isA() == deviceType )
{
Event theEvent;
theEvent.type = S_DEVICE;
theEvent.typeCode = D_POSITION;
theEvent.point.row = row;
theEvent.point.column = column;
theDevice.processA( theEvent );
}
}
delete &device;
}
// End EventManager::positionDevice //
// Member Function //
void EventManager::hideDevice( classType deviceType )
// Description -------------------------------------------------------------
//
// The requested device will be hidden if it is in the list
//
// End ---------------------------------------------------------------------
{
ContainerIterator& device = deviceList.initIterator();
while( ( int )device )
{
Device& theDevice = ( Device& )device++;
if( deviceType == theDevice.isA() )
{
Event theEvent;
theEvent.type = S_DEVICE;
theEvent.typeCode = D_HIDE;
theDevice.processA( theEvent );
}
}
delete &device;
}
// End EventManager::hideDevice //
// Member Function //
void EventManager::showDevice( classType deviceType )
// Description -------------------------------------------------------------
//
// The requested device will be revealed if it is in the list
//
// End ---------------------------------------------------------------------
{
ContainerIterator& device = deviceList.initIterator();
while( ( int )device )
{
Device& theDevice = ( Device& )device++;
if( deviceType == theDevice.isA() )
{
Event theEvent;
theEvent.type = S_DEVICE;
theEvent.typeCode = D_SHOW;
theDevice.processA( theEvent );
}
}
delete &device;
}
// End EventManager::showDevice //
// Member Function //
void EventManager::addDevice( Object& theDevice )
// Description -------------------------------------------------------------
//
// Simple method for adding your device to the list for polling
//
// End ---------------------------------------------------------------------
{
deviceList.add( theDevice );
( ( Device& )theDevice ).theEventManager = this;
Event theEvent;
// Now we need to initialize our device
theEvent.type = S_DEVICE;
theEvent.typeCode = D_INIT;
( ( Device& )theDevice ).processA( theEvent );
}
// End EventManager::addDevice //
// Member Function //
void EventManager::subtractDevice( Object& theDevice )
// Description -------------------------------------------------------------
//
// Simple method for subtracting your device to the list for polling
//
// End ---------------------------------------------------------------------
{
deviceList.detach( theDevice );
( ( Device& )theDevice ).theEventManager = 0;
}
// End EventManger::subtractDevice //